#include "dwarf_utils.h"

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#include <iostream>
#include <iomanip>
#include <sstream>

#include "elog.h"
#include "elog_file.h"

int print_time() {
    timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);

    std::time_t now_c = ts.tv_sec;
    std::tm now_tm = *std::localtime(&now_c);

    std::cout << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "." << std::setfill('0') << std::setw(3) << ts.tv_nsec / 1000000 << std::endl;

    return 0;
}

std::string getTimeStamp() {
    //获取当前时刻的时间戳
    auto now = std::chrono::system_clock::now();

    //将时间戳表示为当地日期和时间
    std::time_t t = std::chrono::system_clock::to_time_t(now);
    std::tm tm = *std::localtime(&t);
  
    //获取毫秒部分
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
      now.time_since_epoch()).count() % 1000;

    //格式化时间戳的字符串表示
    std::ostringstream oss;
    oss << std::put_time(&tm, "%Y-%m-%d-%H-%M-%S") << "-" << std::setw(3) << std::setfill('0') << ms;

    return oss.str();
}

std::string getTimeStamp(std::chrono::system_clock::time_point time_point, const char* timestamp_format) {
    //将时间戳表示为当地日期和时间
    std::time_t t = std::chrono::system_clock::to_time_t(time_point);
    std::tm tm = *std::localtime(&t);
  
    //获取毫秒部分
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
      time_point.time_since_epoch()).count() % 1000;

    //格式化时间戳的字符串表示
    std::ostringstream oss;
    oss << std::put_time(&tm, timestamp_format) << "-" << std::setw(3) << std::setfill('0') << ms;

    return oss.str();
}

double getSteadyTimeToSystemTimeOffset() {
    struct timeval system_time;
    struct timespec steady_time;

    gettimeofday(&system_time, NULL);
    clock_gettime(CLOCK_MONOTONIC, &steady_time);

    double system_time_s =  (double)system_time.tv_sec + double(system_time.tv_usec / 1000000.0);
    double steady_time_s = (double)steady_time.tv_sec + double(steady_time.tv_nsec / 1000000000.0);
    
    return system_time_s - steady_time_s;
}

std::string steadyTimeToSystemTime(const struct timeval& steady_time) {
    double steady_time_s = (double)steady_time.tv_sec + double(steady_time.tv_usec / 1000000.0);
    double system_time_s = steady_time_s + getSteadyTimeToSystemTimeOffset();

    struct timeval system_time;
    system_time.tv_sec = (long)system_time_s;
    system_time.tv_usec = (long)((system_time_s - (double)system_time.tv_sec) * 1000000.0);

    std::time_t now_c = system_time.tv_sec;
    std::tm tm = *std::localtime(&now_c);
    
    //格式化时间戳的字符串表示
    std::ostringstream oss;
    oss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%S") << "." << std::setfill('0') << std::setw(3) << system_time.tv_usec / 1000;

    // log_i("stamp:%s", oss.str().c_str());

    return oss.str();
}

//tf卡检测
int checkSdExist() {
	FILE *fp;
    char line[100];
    fp = popen("fdisk -l", "r");
	if (fp != NULL) {
		while (fgets(line, 100, fp) != NULL) {
			if(strstr(line, " /dev/mmcblk2")) {
				pclose(fp);
				return 0;
			}
        }
    
        pclose(fp);
    }

	return -1;
}

long getTimestampInSeconds() {
    struct timespec uptime;

    // 获取相对于系统启动时间的时间戳
    if (clock_gettime(CLOCK_MONOTONIC, &uptime) == -1) {
        log_e("Error getting uptime");
        return -1;
    }

    return static_cast<long>(uptime.tv_sec);
}

std::time_t getSecondsTimestamp() {
    // 获取当前时间点  
    auto now = std::chrono::system_clock::now();
    // 转换为自 Unix 纪元以来的秒数
    std::time_t seconds = std::chrono::system_clock::to_time_t(now);
    return seconds;
}